package Interceptor;

import com.jfinal.core.Controller;
import com.jfinal.kit.Kv;
import com.jfinal.validate.Validator;
import model.User;

import java.util.List;

public class RegeistValidator extends Validator {

    private String errorKey = "error";

    @Override
    protected void validate(Controller controller) {
        String sql = "select * from t_user";
        List<User> users = User.dao.find(sql);
        for(User user : users){
            if (controller.getPara("username").equals(user.getUsername())){
                addError(errorKey,"用户名重复");
                break;
            }
        }
    }

    @Override
    protected void handleError(Controller controller) {
        String message = controller.getAttr(errorKey);
        Kv result = Kv.by("success", false).set("message", message);
        controller.renderJson(result);
    }
}
